package com.example.liuke.guangguang.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.liuke.guangguang.R;
import com.example.liuke.guangguang.myweidgt.MyLinearLayout2;



public class DongtaiAddViewActivity extends AppCompatActivity {
    private MyLinearLayout2 myLinearLayout2;
    private Button mybutton, delet;
    private int i = 0;//int left, int top, int right, int bottom
    private int with = 10, height = 10,left=15,right=15,top=15,bottom=15;
    private RelativeLayout addview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dongtai_add_view);
        mybutton = (Button) findViewById(R.id.add);
        delet = (Button) findViewById(R.id.delet);
        myLinearLayout2 = (MyLinearLayout2) findViewById(R.id.addview);
        addview1= (RelativeLayout) findViewById(R.id.addview1);
        mybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addtextview();
            }
        });
        delet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myLinearLayout2.removeAllViews();
            }
        });
    }

    private void addtextview() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        left+=left;
        top+=top;
        right+=right;
        bottom+=bottom;

        lp.setMargins(left++,top++,right++,bottom++);
        LinearLayout view = new LinearLayout(this);
        RelativeLayout.LayoutParams linp = new RelativeLayout.LayoutParams(
                40, 20);
        linp.setMargins(left++,top++,right++,bottom++);
        view.setBackgroundColor(Color.parseColor("#FF4081"));

        view.setLayoutParams(linp);//设置布局参数
        view.setOrientation(LinearLayout.HORIZONTAL);

        ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tv1 = new TextView(DongtaiAddViewActivity.this);
        tv1.setLayoutParams(linp);
        tv1.setText((i++) + "");
        tv1.setWidth(with++);
        tv1.setHeight(height++);
        tv1.setBackgroundResource(R.drawable.btn_bookmark);
        tv1.setBackgroundColor(Color.parseColor("#FF4081"));


        ImageView applicationImageView = new ImageView(this);


        applicationImageView.setBackgroundResource(R.drawable.btn_bookmark);
        applicationImageView.setLayoutParams(lp);
        addview1.addView(tv1);


        //addview1.addView(view);
    }
}
